<template>
	<view>
		<uni-popup ref="show_app_auth" type="center" :custom="true">
			<view class="common-popup-content fill-base pd-lg radius-34">
				<view class="title">权限说明</view>
				<view class="desc">
					{{text || typeArr[type]}}
				</view>
				<view class="button">
					<view @tap.stop="toConfirmAppAuth('cancel')" class="item-child">禁止</view>
					<view @tap.stop="toConfirmAppAuth('confirm')" class="item-child c-base"
						:style="{background: primaryColor,color:'#fff'}">允许</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import permision from '@/utils/permission.js'
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		props: {
			type: {
				type: String,
				default () {
					return 'location'
				}
			},
			text: {
				type: String,
				default () {
					return ''
				}
			},
		},
		mounted() {},
		data() {
			return {
				typeArr: {
					location: '对定位权限申请说明：便于您使用该功能获取位置信息，用于获取当前位置信息、选择地址等',
					phone: '对拨打电话权限申请说明：便于您使用该功能拨打电话，用于联系客服',
					save: '对存储权限申请说明：便于您使用该功能保存图片到相册',
					album: '对读取存储中的图片/视频权限申请说明：便于您使用该功能上传您的照片/图片/视频，用于更换用户头像、服务人员资格证书、工作形象照、个人生活照、个人视频介绍等、申述/退款时提交反馈图片等',
					camera: '对相机/摄像头权限申请说明：便于您使用该功能上传您的照片/图片/视频，用于更换用户头像、服务人员资格证书、工作形象照、个人生活照、个人视频介绍等、申述/退款时提交反馈图片等'
				}
			}
		},
		computed: mapState({}),
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo']),
			...mapMutations(['updateUserItem']),
			open() {
				this.$refs.show_app_auth.open()
			},
			close() {
				this.$refs.show_app_auth.close()
			},
			async toConfirmAppAuth(key) {
				let {
					appCheckLocaStatus = null,
						type
				} = this
				this.close()
				if (key === 'cancel') {
					this.$emit(key)
					return
				}
				let typeArr = {
					location: 'android.permission.ACCESS_FINE_LOCATION',
					phone: 'android.permission.CALL_PHONE',
					save: 'android.permission.WRITE_EXTERNAL_STORAGE',
					camera: 'android.permission.CAMERA',
					album: 'android.permission.READ_EXTERNAL_STORAGE'
				}
				let status = await permision.requestAndroid(typeArr[type])
				if (status === null || status === 1) {
					status = 1
				}
				if (status === 1) {
					this.$emit(key)
					return
				}
				if (status === -1) {
					if ((type == 'location' && appCheckLocaStatus == status) || type != 'location') {
						permision.gotoAppSetting()
					}
					if (type == 'location') {
						this.updateUserItem({
							key: 'locaRefuse',
							val: false
						})
					}
					return
				}
			}
		},
	}
</script>

<style scoped lang="scss">
</style>